package com.seanzx;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class GamePanel extends JPanel implements KeyListener, ActionListener {

    int length;
    int[] snakeX = new int[600];
    int[] snakeY = new int[500];
    String direction;

    boolean isStart;

    Timer timer = new Timer(100, this);

    int foodX;
    int foodY;
    Random random = new Random();

    public GamePanel() {
        init();
        this.setFocusable(true);
        this.addKeyListener(this);
    }

    public void init() {
        length = 3;
        snakeX[0] = 100;
        snakeY[0] = 100;
        snakeX[1] = 75;
        snakeY[1] = 100;
        snakeX[2] = 50;
        snakeY[2] = 100;
        isStart = false;
        direction = "r";
        timer.start();

        foodX = 25 + 25 * random.nextInt(34);
        foodY = 75 + 25 * random.nextInt(24);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.setBackground(Color.WHITE);
        Data.HEADER.paintIcon(this, g, 25, 11);
        g.fillRect(25, 75, 850, 600);

        switch (direction) {
            case "r":
                Data.RIGHT.paintIcon(this, g, snakeX[0], snakeY[0]);
                break;
            case "l":
                Data.LEFT.paintIcon(this, g, snakeX[0], snakeY[0]);
                break;
            case "u":
                Data.UP.paintIcon(this, g, snakeX[0], snakeY[0]);
                break;
            case "d":
                Data.DOWN.paintIcon(this, g, snakeX[0], snakeY[0]);
            default:
        }
        for (int i = 1; i < length; i++) {
            Data.BODY.paintIcon(this, g, snakeX[i], snakeY[i]);
        }

        Data.FOOD.paintIcon(this, g, foodX, foodY);

        if (!isStart) {
            g.setColor(Color.WHITE);
            g.setFont(new Font("微软雅黑", Font.BOLD, 40));
            g.drawString("按下空格开始游戏", 300, 300);
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_SPACE) {
            isStart = !isStart;
            repaint();
        }
        switch (e.getKeyCode()) {
            case KeyEvent.VK_LEFT:
                direction = "l";
                break;
            case KeyEvent.VK_RIGHT:
                direction = "r";
                break;
            case KeyEvent.VK_UP:
                direction = "u";
                break;
            case KeyEvent.VK_DOWN:
                direction = "d";
                break;
            default:
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (isStart) {
            for (int i = length - 1; i > 0; i--) {
                snakeX[i] = snakeX[i - 1];
                snakeY[i] = snakeY[i - 1];
            }
            switch (direction) {
                case "r":
                    snakeX[0] += 25;
                    if (snakeX[0] >= 875) {
                        snakeX[0] = 25;
                    }
                    break;
                case "l":
                    snakeX[0] -= 25;
                    if (snakeX[0] <= 0) {
                        snakeX[0] = 850;
                    }
                    break;
                case "u":
                    snakeY[0] -= 25;
                    if (snakeY[0] <= 50) {
                        snakeY[0] = 650;
                    }
                    break;

                case "d":
                    snakeY[0] += 25;
                    if (snakeY[0] >= 675) {
                        snakeY[0] = 75;
                    }
                default:
            }
            if (snakeX[0] == foodX && snakeY[0] == foodY) {
                foodX = 25 + 25 * random.nextInt(34);
                foodY = 75 + 25 * random.nextInt(24);
                Data.FOOD.paintIcon(this, this.getGraphics(), foodX, foodY);
                snakeX[length] = snakeX[length - 1];
                snakeY[length] = snakeY[length - 1];
                length ++;
            }
            repaint();
        }
    }
}
